package mulMan.services;

import java.util.List;

import mulMan.dao.PlaylistDAO;
import mulMan.domain.AudioFile;
import mulMan.domain.Playlist;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Property;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;

public class PlaylistServicesImpl implements PlaylistServices, ApplicationContextAware {
	
	private PlaylistDAO playlistDAO;
	private ApplicationContext applicationContext;
	
	public void setPlaylistDAO(PlaylistDAO playlistDAO) {
		this.playlistDAO = playlistDAO;
	}
	
	
	@Transactional
	public void savePlaylist(Playlist playlist) throws Exception {
		try {
			playlistDAO.savePlaylist(playlist);
			System.out.println("Playlist inserted with [id: " + playlist.getIdPlaylist() + 
					"] [name: " + playlist.getName() + "]");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to insert a playlist into the database:" + dae);
		}
	}

	@Transactional
	public List<Playlist> getPlaylists() throws Exception{
		try {
			List<Playlist> playlistList = playlistDAO.getPlaylists();
			
			for (Playlist playlist : playlistList) {
				playlist.setPlaylistServices((PlaylistServices) applicationContext.getBean("playlistServices"));
				
				for (AudioFile audioFile : playlist.getAudioFiles()) {
					audioFile.setAudioFileServices((AudioFileServices) applicationContext.getBean("audioFileServices"));
				}
			}
			
			System.out.println(playlistList.size() + " playlists records recovered from the database");
			return playlistList;
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to get the list of playlists from the database:" + dae);
		}
	}

	@Transactional
	public Playlist getPlaylist(int idPlaylist) throws Exception {
		try {
			Playlist playlist = playlistDAO.getPlaylist(idPlaylist);
			
			playlist.setPlaylistServices((PlaylistServices) applicationContext.getBean("playlistServices"));
			
			for (AudioFile audioFile : playlist.getAudioFiles()) {
				audioFile.setAudioFileServices((AudioFileServices) applicationContext.getBean("audioFileServices"));
			}
			
			System.out.println("Playlist record recovered from the database with [id: " + playlist.getIdPlaylist() + 
					"] [name: " + playlist.getName() + "]");
			
			return playlist;
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to get the playlist from the database:" + dae);
		}
	}

	@Transactional
	public void updatePlaylist(Playlist playlist) throws Exception {
		try {
			playlistDAO.updatePlaylist(playlist);
			System.out.println("Playlist updated with [id: " + playlist.getIdPlaylist() + 
					"] [name: " + playlist.getName() + "]");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to update a playlist into the database:" + dae);
		}
	}

	@Transactional
	public void deletePlaylist(Playlist playlist) throws Exception {
		try {
			playlistDAO.deletePlaylist(playlist);
			System.out.println("Playlist successfuly deleted");
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to delete a playlist from the database:" + dae);
		}
	}
	
	@Transactional
	public Playlist getMainPlaylist() throws Exception {
		try {
			Criterion mainCriterion = Property.forName("main").like(1);
			Playlist playlist = playlistDAO.getMainPlaylist(mainCriterion);
			
			playlist.setPlaylistServices((PlaylistServices) applicationContext.getBean("playlistServices"));
			
			for (AudioFile audioFile : playlist.getAudioFiles()) {
				audioFile.setAudioFileServices((AudioFileServices) applicationContext.getBean("audioFileServices"));
			}
			
			System.out.println("Playlist record recovered from the database with [id: " + playlist.getIdPlaylist() + 
					"] [name: " + playlist.getName() + "]");
			
			return playlist;
		} catch (DataAccessException dae) {
			throw new Exception("An error occured while trying to get the playlist from the database:" + dae);
		}
	}


	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		if (applicationContext == null) {
			applicationContext = arg0;
		}
	}

}
